import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JOptionPane;


public class ParkitDriver {
	private Vector<ParkitUser> users;
	private Vector<Spot> spots;
	
	public ParkitDriver()
	{
    users = new Vector<ParkitUser> ();
	}
	
	/**
	 * 
	 * @param l1 long
	 * @param l2 lat
	 * @return
	 */
	public String[] spotByGPS(float l1, float l2)
	{
		ParkitUser user = new ParkitUser();
		for(ParkitUser u: users)
		{
			if ((u.getLat() == l1) && (u.getLng() == l2))
			user = u;
		}
		int i = 0;
		String[] s = new String[3];
		if (user.getOpenSpot() == null)
		{
			return null;
		}
		while (i < 3)
		{
			s[i] = user.getOpenSpot().toString();
			i++;
		}
		return s;
	}
	
	/**
	 * 
	 * @param l1
	 * @param l2
	 * @param b # times method has been called
	 * @return
	 */
	public String getNextSpot(float l1, float l2, int b)
	{
		ParkitUser user = new ParkitUser();
		for(ParkitUser u: users)
		{
			if ((u.getLat() == l1) && (u.getLng() == l2))
			user = u;
		}
		Spot p = user.getOpenSpot(b);
		return "The nearest spot is " ;
	}
	
	public Vector<Spot> getSpots()
	{
		return spots;
	}
	
	public Boolean userExists(float l1, float l2)
	{
		ParkitUser temp = new ParkitUser(l1, l2);
		if (users == null)
		  return false;
		
		for(ParkitUser u: users)
		{
			if (u.equals(temp))
			{
				return true;
			}
		}
		return false;
	}
	
	public int getIndexUser(float l1, float l2)
	{
		ParkitUser temp = new ParkitUser(l1, l2);
		for(ParkitUser u: users)
		{
			if (u.equals(temp))
			{
				return users.indexOf(u);
			}
		}
		return -1;
	}
	
	public void initUsers()
	{
		for(Spot s:spots)
		{
		  System.err.println (s.getLat());
      System.err.println (s.getLng());
      
			if (userExists(s.getLat(), s.getLng()))
			{
				int u = getIndexUser(s.getLat(), s.getLng());
				if(u == -1)
				{
					System.out.println("error");
				}
				else{		
				ParkitUser user = users.get(u);
				user.addSpot(s);}
			}
			else
			{
			  ParkitUser u = new ParkitUser(s.getLat(), s.getLng());
			  users.add(u);
			  
			}
		}
	}
	
	public Vector<ParkitUser> getUsers()
	{
		return users;
	}
	
	public void initSpots(Vector<Spot> v)
	{
		spots = v;
	}
	
	public static void main(String[] args)
	{
		ParkitDriver p = new ParkitDriver();
		
		   PopulateSpots populate = new PopulateSpots ();

		    Vector<Spot> spotsVec = populate.getSpots();
		    
		    for (int i = 0; i < spotsVec.size(); i++)
		    {
		      Spot sp = spotsVec.get(i);
		      
		      int id = sp.getMmId();
		      String st = sp.getStName();
		      String from = sp.getStFrom();
		      String to = sp.getStTo();
		      
		      boolean isavail = sp.isOpen();
		      
		      int userid = 1234;
		      sp.occupy(userid);
		      
		      sp.free ();
		      
		      
		    }
		    
		    p.initSpots(spotsVec);
		    p.initUsers();
		    for(Spot s: p.getSpots())
		    {
		    	int i = 0;

	    		String[] str = new String[3];
	    	str = p.spotByGPS(s.getLat(), s.getLng());
	    	if(str == null)
	    	{
	    		System.out.println("No open spots :(");
	    	}
	    	else{
		    	while (i < 3){
		    		System.out.println(str[i]);
		    		i++;
		    }}
		   
		    }
	}
}
